package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduQuestionBankMapper;
import com.zenith.edu.po.domain.EduQuestionBank;
import com.zenith.edu.po.dto.EduQuestionBankDTO;
import com.zenith.edu.po.vo.EduQuestionBankVO;
import com.zenith.edu.service.EduQuestionBankService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduQuestionBankServiceImpl extends ServiceImpl<EduQuestionBankMapper, EduQuestionBank> implements EduQuestionBankService {
    
    @Override
    public EduQuestionBankVO createQuestionBank(EduQuestionBankDTO dto) {
        EduQuestionBank questionBank = new EduQuestionBank();
        BeanUtils.copyProperties(dto, questionBank);
        this.save(questionBank);
        
        EduQuestionBankVO vo = new EduQuestionBankVO();
        BeanUtils.copyProperties(questionBank, vo);
        return vo;
    }
    
    @Override
    public EduQuestionBankVO getQuestionBankById(Long id) {
        EduQuestionBank questionBank = this.getById(id);
        if (questionBank == null) {
            return null;
        }
        
        EduQuestionBankVO vo = new EduQuestionBankVO();
        BeanUtils.copyProperties(questionBank, vo);
        return vo;
    }
    
    @Override
    public EduQuestionBankVO updateQuestionBank(EduQuestionBankDTO dto) {
        EduQuestionBank questionBank = new EduQuestionBank();
        BeanUtils.copyProperties(dto, questionBank);
        this.updateById(questionBank);
        
        EduQuestionBankVO vo = new EduQuestionBankVO();
        BeanUtils.copyProperties(questionBank, vo);
        return vo;
    }
    
    @Override
    public boolean deleteQuestionBank(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduQuestionBankVO> getQuestionBankList() {
        List<EduQuestionBank> questionBankList = this.list();
        return questionBankList.stream().map(questionBank -> {
            EduQuestionBankVO vo = new EduQuestionBankVO();
            BeanUtils.copyProperties(questionBank, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}